/**
 * 
 */
define([],function(){
	
	var dataModelGroupCom=function(params){
		params=$.extend({
			onClick:null,
			render:null
		},params);
		var that=this;
		var uuid=webUtils.uuid();
		
		var btTemp=
		'<div class="btn-group">'
		+'<a href="javascript:void(0)" class="btn btn-sm btn-success addBt">'
		+'<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>添加'
		+'</a>'
		+'<a href="javascript:void(0)" class="btn btn-sm btn-primary editBt">'
		+'<span class="glyphicon glyphicon-edit" aria-hidden="true"></span>修改'
		+'</a>'
		+'<a href="javascript:void(0)" class="btn btn-sm btn-danger delBt">'
		+'<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>删除'
		+'</a>'
		+'</div>'
		
		
		params.render.find(".box-tools").append(btTemp);
		this.temp=params.render.find(".box-tools");
		
		params.render.find(".box-body").append("<div id='tree_"+uuid+"' class='ztree'></div>");
		
		var _dataModelGroupTree = params.render.find("#tree_"+uuid);
		
		_dataModelGroupTree = $.fn.zTree.init(_dataModelGroupTree, {
	        treeId: "_dataModelGroupTree",
	        async: {
	            enable: true,
	            url: ctx + "/dataDicAction/queryDataDicItemTreeNodes.do?dataDicCode=DATAMODEL_GROUP",
	            autoParam: ["id=pid"],
	            dataFilter: function (treeId, parentNode, responseData) {
	                if (responseData) {
	                    $.each(responseData, function (i, n) {
	                        if (n.childrenSize > 0) {
	                            n.isParent = true;
	                        }
	                    });
	                }
	                return responseData;
	            }
	        },
	        data: {
	            key: {
	                name: "text"
	            }
	        },
	        edit: {
	        },
	        callback: {
	            onClick: function (e, treeId, treeNode, clickFlag) {
	                if (clickFlag != 0) {
	                	that.temp.find(".dataModelBtnText").html(treeNode.name);
	                    if(params.onClick){
	                    	params.onClick.call(that,treeNode);
	                    }
	                }
	            }
	        }
	    }, [{
	            text: "业务分组",
	            id: "-1",
	            isParent: true,
	            open: true
	        }]);
		_dataModelGroupTree.reAsyncChildNodes(_dataModelGroupTree.getNodesByParam("id", "-1")[0], "refresh");
		
				
		this.temp.find(".addBt").click(function(){
			 var nodes = _dataModelGroupTree.getSelectedNodes();
	            var pid = '-1';
	            if (nodes && nodes.length > 0) {
	                var node = nodes[0];
	                pid = node.id;
	            }
	            var sub = top.webUtils.topicSubscribe("dataDicItem.edit.success", function (args) {
	            	var cnode = args.obj;
	            	_dataModelGroupTree.addNodes(node, cnode);
	                dialog.close();
	            });
	            var dialog = editDataModelGroup({
	                pid: pid,
	                onClose: function () {
	                    sub.remove();
	                }
	            });
		});
		

		this.temp.find(".editBt").click(function(){
			var nodes = _dataModelGroupTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
                var node = nodes[0];
                var sub = top.webUtils.topicSubscribe("dataDicItem.edit.success", function (args) {
                    var cnode = args.obj;
                    _dataModelGroupTree.updateNode($.extend(node, cnode));
                    dialog.close();
                });
                var dialog = editDataModelGroup({
                    id: node.id,
                    onClose: function () {
                        sub.remove();
                    }
                });
            } else {
            	webUtils.alert("请选择其中一个");
            }
		});
		
		this.temp.find(".delBt").click(function(){
			var nodes = _dataModelGroupTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
            	webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var node = nodes[0];
		                var ids = [];
		                ids.push(node.id);
		                $.post(ctx + "/dataDicAction/delDataDicItems.do", {ids: ids.join(",")}, function (data) {
		                    if (data.success) {
		                    	_dataModelGroupTree.removeNode(node);
		                    } else {
		                        webUtils.error(data.msg);
		                    }
		                }, 'json');
					}
        		});

            } else {
            	webUtils.alert("请选择需要删除的");
            }
		});
		
		var editDataModelGroup = function (params) {
            params = params || {};
            var t = [];
            if (params.id) {
                t.push("id=" + params.id);
            }
            if (params.pid) {
                t.push("pid=" + params.pid);
            }
            t.push("dataDicCode=DATAMODEL_GROUP");
            
            var dialog=webUtils.dialog({
           	 title:'编辑系统',
           	 url:ctx + '/dataDicAction/dataDicItemEditView.do?' + t.join("&"),
           	 onremove:function () {
                    if (params.onClose) {
                        params.onClose.call(null);
                    }
                }
            });
            return dialog;
        };
	};
	
	dataModelGroupCom.prototype.getCom=function(){
		return this.temp;
	}
	
	return dataModelGroupCom;
});